/**
* 工程名: 	Quickdisk
* 文件名: 	DownloadFile.java
* 创建人:  	guo.tianxiang
* 创建时间: 	2014-4-17 下午2:39:07
* 版权所有：	Copyright (c) 2014 苏州科大恒星信息技术有限公司  
* 文件描述: 描述该文件的作用
* -----------------------------变更记录 ----------------------------- 
* 日期        		变更人      		版本号  		变更描述  
* ------------------------------------------------------------------  
* 2014-4-17     guo.tianxiang   	1.0       	first created  
*/
package com.tianxiang.action;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.tianxiang.model.User;
import com.tianxiang.service.ManageFileService;
import com.tianxiang.utils.Tools;

/**
 * 文件下载Action
 *  @since Quickdisk 1.1
 * @version 1.0 2014-4-17
 * @author guo.tianxiang
 */
public class DownloadFileAction extends ActionSupport {
	/**
	* serialVersionUID: (用一句话描述这个变量表示什么)
	*/
	private static final long serialVersionUID = 8361455189561150669L;
	static  Log log= LogFactory.getLog(DownloadFileAction.class); 
	private String  fileName;		//要下载的文件文件名
	private String  filePath;			//要下载文件所在父文件夹
	
	public String getFileName() {
		System.out.println("调用getFileName:"+fileName);
		return fileName;
	}

	public void setFileName(String fileName) throws UnsupportedEncodingException {
		System.out.println("调用setFileName:"+fileName);
			this.fileName = fileName;
		//	fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1"); 
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
			this.filePath = filePath;
	}

	/** 
     * 获取下载显示的文件名称 
     */  
    public String getDownloadFileName() {  
        String downFileName = fileName;  
        System.out.println("转码前："+downFileName);
        try {  
            downFileName = new String(downFileName.getBytes(), "ISO8859-1");  
        } catch (UnsupportedEncodingException e) {  
            log.error("获取下载显示的文件名称",e);  
        }  
        System.out.println("转码后："+downFileName);
        return downFileName;  
    }  
  
    String getPath(){
    	String path = null;
    	try {
			path=new String(filePath.getBytes(),"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	return path;
    }
    /**
     * 
    * 返回给struts.xml配置中的name,这是在Streamresult中处理的
    * 不管项目是使用什么编码的，都是使用ISO8859-1编码
    * @return
    * @throws UnsupportedEncodingException
     */
    public String getName() throws UnsupportedEncodingException {
		return new String(getFileName().getBytes(),"ISO8859-1");
	}
    /** 
     *  返回给客户端的下载流
     * @return 
     * @throws FileNotFoundException  
     */  
    public InputStream getInputStream()  {  
    	User user=(User)ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
String username=user.getUsername();
  //      String realpath = ServletActionContext.getServletContext().getRealPath(filePath);  
System.out.println("文件下载父目录是："+filePath);
		ManageFileService manageFileService= new ManageFileService();
		String realpath=manageFileService.getFile(username, fileName).getPath();
    //老方法，已不适用   String realpath=new File(Tools.getAcessPath(username,"", filePath),fileName).getAbsolutePath();
       	System.out.println("要下载的路径是："+realpath);
          BufferedInputStream bStream = null;
		try {
			bStream = new BufferedInputStream(new FileInputStream(realpath));
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		if (bStream==null) {
			System.out.println("Stream is null");
		};
		System.out.println("返回Stream……");
        return bStream ;  

    }  
     public String execute() throws Exception {  
            return SUCCESS;  
        }
}
